package ss.pku.Scope;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.SimpleName;

import ss.pku.SymbolTable.ScopeVisitor;
import ss.pku.util.GlobalContainer;

/**
 * every method maps to this model
 * 
 * @author liangwei
 * 
 */
public class MethodScope extends AbstractScope {

	private String methodName = "";
	private MethodDeclaration method;
	private List parameters;
	private String returnType = "";
	private LocalScope localScope;

	// TODO xuxu, add the variables in this field
	public List<Object> variables = new ArrayList<Object>();
	public static HashMap<SimpleName, List<Object>> map = new HashMap<SimpleName, List<Object>>();

	public MethodScope(MethodDeclaration method, CompilationUnit cu) {
		System.out.println("*******************************************");

		this.method = method;
		if (method.getName() != null)
			this.methodName = method.getName().toString();
		/**
		 * this String can get by Type can have type binding, may be great
		 * further TODO
		 */
		if (method.getReturnType2() != null)
			this.returnType = method.getReturnType2().toString();
		this.parameters = method.parameters();
		if (GlobalContainer.times++ == 0) {
			ScopeVisitor visitor = new ScopeVisitor(this);
			cu.accept(visitor);
			this.localScope = new LocalScope(cu);
		}
	}

	public LocalScope getLocalScope() {
		return localScope;
	}

	public String getMethodName() {
		return methodName;
	}

	public MethodDeclaration getMethod() {
		return method;
	}

	public List getParameters() {
		return parameters;
	}

	public String getReturnType() {
		return returnType;
	}

	/**
	 * get the location of this scope
	 * 
	 */
	public int getStartLocation() {
		return this.method.getStartPosition();
	}

	public int getEndLocation() {
		return this.method.getStartPosition() + this.method.getLength();
	}

	public int getLength() {
		return this.method.getLength();
	}

}
